import {Object3D,Scene,PerspectiveCamera,Vector3} from 'three';
import Constant from './ui-constant';

export default class UIContainer extends Object3D{
    constructor(options={}){
        super();
        this._init(options);
    }

    get uiType(){
        return Constant.TYPE_COMPONENT_CONTAINER;
    }
    get isTop(){
        return this._isTop;
    }
    set isTop(bol){
        this._isTop=bol;
    }
    get scene(){
        return this._scene;
    }
    set scene(scn){
        this._scene=scn;
        this._scene.add(this);
    }
    get camera(){
        return this._camera;
    }
    set camera(cam){
        this._camera=cam;
    }
    get layout(){
        return this._layout;
    }
    set layout(layout_type){
        this._layout=layout_type===Constant.LAYOUT_ABSOLUTE?layout_type:Constant.LAYOUT_RELATIVE;
    }
    get childUis(){
        return this._childUIs;
    }
    // get isUI(){
    //     return true;
    // }

    getOffset(out=new Vector3()){
        out.copy(this._uiGroup.position);
        return out;
    }
    setOffset(x,y,z){
        this._uiGroup.position.set(x,y,z);
    }
    addUI(...els){
        for(let el of els){
            let idx=this._childUIs.findIndex(ui=>ui.uuid===el.uuid);
            if(idx>-1)continue;
            this._uiGroup.add(el);
            this._childUIs.push(el);
        }
    }
    removeUI(...els){
        for(let el of els){
            let idx=this._childUIs.findIndex(ui=>ui.uuid===el.uuid);
            if(idx<0)continue;
            this._uiGroup.remove(el);
            this._childUIs.splice(idx,1);
        }
    }
    reset(){
        let _mat=this.matrixWorld.clone();
        _mat.invert();
        this.applyMatrix4(_mat);
    }

    _init(options){
        this._initVariable(options);
        this._initialize(options);
    }
    _initVariable(options){
        this._scene=options.scene||new Scene();
        this._camera=options.camera||new PerspectiveCamera(50,2,0.1,10);
        this._layout=options.layout||Constant.LAYOUT_ABSOLUTE;
        this._isTop=!!options.isTop;
        this._childUIs=[];
        this._uiGroup=new Object3D();
    }
    _initialize(options){
        this._scene.add(this);
        this.add(this._uiGroup);
        this._uiGroup.position.copy(options.offset||Constant.DEFAULT.UI_OFFSET);
        // Object.defineProperty(this._uiGroup,'isUI',{value:true});
    }

}